package com.yanqu.road.server;

import com.yanqu.road.NettyMgr;
import com.yanqu.road.dao.db.DBTableInfoMgr;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.manager.CrossDataAnalyticsMgr;
import com.yanqu.road.server.manager.banquet.*;
import com.yanqu.road.server.manager.config.*;
import com.yanqu.road.server.manager.log.CrossBanquetLogMgr;
import com.yanqu.road.server.manager.marriage.CrossGroupMarriageMgr;
import com.yanqu.road.server.manager.mogulbirthday.MogulBirthdayMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.*;
import com.yanqu.road.server.timer.CenterBanquetTimerMgr;
import com.yanqu.road.utils.config.Config;
import org.apache.logging.log4j.LogManager;

import java.util.ArrayList;
import java.util.List;

public final class CrossBanquetServer extends CrossBaseServer {

    private static CrossBanquetServer crossBanquetServer;

    public static CrossBanquetServer getInstance() {
        if (null == crossBanquetServer) {
            crossBanquetServer = new CrossBanquetServer();
            if (null == crossBaseServer) {
                crossBaseServer = crossBanquetServer;
            }else {
                logger.error("CrossBanquetServer getInstance baseServer exist");
            }
        }
        return crossBanquetServer;
    }

    @Override
    public boolean stop() {
        try {
            super.stop();
            //保存mgr数据
            for (TempMgr tempMgr : CrossBanquetServer.getInstance().getMgrList()) {
                try {
                    tempMgr.save();
                }catch (Exception e){
                    logger.error("mgr save error:", e);
                }
            }
        } catch (Exception e) {
            logger.error("CrossBanquetServer close error", e);
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        if (args.length <= 0) {
            System.err.println("check set config path");
            return;
        }

        Config.ParseArgs(args);
        cfgPath = args[0];

        CrossBanquetServer crossBanquetServer = CrossBanquetServer.getInstance();
        crossBanquetServer.setServerType(Integer.valueOf(args[1]));
        if (!crossBanquetServer.start(crossBanquetServer.getServerType(), CrossBanquetServer.class.getName())) {
            System.err.println("CrossBanquetServer start fail!");
            System.exit(1);
            return;
        }

        DBTableInfoMgr dbTableInfoMgr = new DBTableInfoMgr();
        dbTableInfoMgr.init(DbNameStrategy.getCross2Db(), "com.yanqu.road.entity.log", true);
        crossBanquetServer.setDbTableInfoMgr(dbTableInfoMgr);

        CenterBanquetTimerMgr.initGeneralTimers(crossBanquetServer.getServerType());
        System.err.println("CrossBanquetServer start success");
        logger.info("CrossBanquetServer start success,cost time:" + (System.currentTimeMillis() - time));
        TempServer.tempServerLogger.info("notify process start success!");


        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            logger.info("CrossBanquetServer 关闭服务器");
            CrossBanquetServer.getInstance().newStop();
        },"ShutdownHook"));

    }

    public void newStop(){
        try {
            Thread.sleep(5000);
            // 关闭netty 避免有新连接
            NettyMgr.newStop();
            // 关闭定时器
            TempServer.getTimerExecutorService().shutdown();
            Thread.sleep(1000);
            // 停止
            for (TempMgr tempMgr : getInstance().getMgrList()) {
                tempMgr.stop();
            }
            // 保存
            for (TempMgr tempMgr : getInstance().getMgrList()) {
                try {
                    tempMgr.save();
                } catch (Exception e) {
                    logger.error("mgr save error:", e);
                }
            }
            // 保存日志
            CrossBanquetLogMgr.save();

            logger.info("CrossBanquetServer 服务器已关闭");
            TempServer.deleteFile("./crossServer.pid");
            super.stopWithLogger();
            LogManager.shutdown();
            System.out.println("CrossBanquetServer 服务器已关闭");
        }catch (Exception e){
            logger.error("CrossBanquetServer stop error", e);
        }
    }


	@Override
	public List<TempMgr> getMgrList() {
        List<TempMgr> mgrList = new ArrayList<>();
        //配置放前面
        mgrList.add(new CrossCheckConfigVersionMgr());
        mgrList.add(new ConfigMgr());
        mgrList.add(new ChannelConfigMgr(sType));
        mgrList.add(new ServerListMgr());
        mgrList.add(new ServerGroupMgr());
        mgrList.add(new ServerGroup2Mgr());
        mgrList.add(new MammonConfigMgr());
        mgrList.add(new CrossServerGroupMgr());
        mgrList.add(new CrossMarriageGroupMgr());
        mgrList.add(new CrossUnionServerGroupMgr());
        mgrList.add(new AgentInterfaceMgr());
        mgrList.add(new SkinMgr());
        mgrList.add(new TempCommonMgr());
        mgrList.add(new TempGoodsBagMgr());
        mgrList.add(new VehicleMgr());
        mgrList.add(new PatronsMgr());
        mgrList.add(new BeautyMgr());
        mgrList.add(new DAServerListMgr());
        mgrList.add(new CrossDataAnalyticsMgr());

        //分组Mgr
        mgrList.add(new CrossUserMgr());
        mgrList.add(new CrossBanquetConfigMgr());
        mgrList.add(new CrossBanquetMgr());
        mgrList.add(new CrossBanquetAutoMgr());
        mgrList.add(new CrossPartyMgr());
        mgrList.add(new CrossBanquetChatMgr());
        mgrList.add(new CrossPartyChatMgr());
        mgrList.add(new CrossGroupMarriageMgr());

        mgrList.add(new MogulBirthdayMgr());

		return mgrList;
	}
}
